package com.malbbmsomanager.reports;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.malbbmsomanager.DM_BBM_InvoiceMenu;
import com.malbbmsomanager.R;
import com.malbbmsomanager.transactions.DM_BBM_InvoiceSalesOrder;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class DM_BBM_Reports_Generator extends Activity implements
ActionBar.OnNavigationListener{
	private String DBNAME = "dminvoicemanagerbbm.db";
	private SQLiteDatabase mydb;
	String TagId;
	EditText etcurrentreport;
	Button btnselectreporttype, btndmreportfromdate, btndmreporttodate,new_report;
	private int mYear, mMonth, mDay, mHour, mMinute;
	static final int DATE_DIALOG_ID_FROM = 0;
	static final int DATE_DIALOG_ID_TO = 1;
	protected static final String TAG = null;
	AlertDialog InvoiceHeaderAlert = null;
	private AlertDialog myDialog;
	String currentReportID, currentReportName;
	int DateTag = 0;
	private String[] items = { "1. SALES SUMMARY DATE WISE","2. SALES SUMMARY INVOICE WISE","3. SALES SUMMARY RETAILER WISE","4. OUTLET DISCOUNT SUMMARY REPORT", "5. OUTLET FREE ISSUE SUMMARY REPORT", };

	// Get Current date and time for Alert Dialog
	public DM_BBM_Reports_Generator() {
		// Assign current Date and Time Values to Variables
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
	}
	// action bar
	private ActionBar actionBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_bbm_reports_generator);

		actionBar = getActionBar();

		// Hide the action bar title
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(false);
		
		Bundle bundle = getIntent().getExtras();
		TagId = bundle.getString("TAGID");

		String[] TagIdCurrentReport = TagId.split("/");
		currentReportID = TagIdCurrentReport[0];
		currentReportName = TagIdCurrentReport[1];

		etcurrentreport = (EditText) findViewById(R.id.etcurrentreport);
		btnselectreporttype = (Button) findViewById(R.id.btnselectreporttype);
		btndmreportfromdate = (Button) findViewById(R.id.btndmreportfromdate);
		btndmreporttodate = (Button) findViewById(R.id.btndmreporttodate);
		new_report= (Button) findViewById(R.id.new_report);
		// ********Display Payment Type List*********
		btnselectreporttype.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				resetFilters();
				myDialog.show();
			}
		});
		new_report.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				resetFilters();
				myDialog.show();
			}
		});
		

		AlertDialog.Builder builder = new AlertDialog.Builder(
				DM_BBM_Reports_Generator.this);
		builder.setNegativeButton("EXIT", null);
		builder.setTitle("SELECT REPORT TYPE");
		builder.setIcon(R.drawable.salessummery);
		builder.setItems(items, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				currentReportName = items[which];
				// String[] accountdetail = selectedReport.split(" ");

				Pattern intsOnly = Pattern.compile("\\d+");
				Matcher makeMatch = intsOnly.matcher(currentReportName);
				makeMatch.find();
				currentReportID = makeMatch.group();
				etcurrentreport.setText(currentReportName.replaceAll(
						"[^a-zA-Z.,]+", ""));
				Log.v(TAG, "currentReportID :"+ currentReportID);
			}
		});

		builder.setCancelable(false);
		myDialog = builder.create();

		// Set ClickListener on btnSelectDate
		btndmreportfromdate.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// Show the DatePickerDialog
				DateTag = 30;
				showDialog(DATE_DIALOG_ID_FROM);
			}
		});

		// Set ClickListener on btnSelectDate
		btndmreporttodate.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// Show the DatePickerDialog
				DateTag = 40;
				showDialog(DATE_DIALOG_ID_TO);
			}
		});

		Button ExitFromReports = (Button) findViewById(R.id.exitfrom_reports);
		ExitFromReports.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(),
						DM_BBM_InvoiceMenu.class);
				myIntent.putExtra("TAGID", "SALES");
				myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivityForResult(myIntent, 0);
				finish();
			}

		});
		Button GenerateReports = (Button) findViewById(R.id.generate_report);
		GenerateReports.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if(etcurrentreport.length() !=0){
					if(currentReportID.equals("1")){
						Intent myIntent = new Intent(view.getContext(),
								DM_BBM_Sales_Summary_DateWise.class);
						startActivityForResult(myIntent, 0);
					}
					else if(currentReportID.equals("2")){
						Intent myIntent = new Intent(view.getContext(),
								DM_BBM_Sales_Summary_InvoiceWise.class);
						startActivityForResult(myIntent, 0);
					}
					else if(currentReportID.equals("3")){
						Intent myIntent = new Intent(view.getContext(),
								DM_BBM_Sales_Summary_RetailerWise.class);
						startActivityForResult(myIntent, 0);
					}
					else if(currentReportID.equals("4")){
						Intent myIntent = new Intent(view.getContext(),
								DM_BBM_Outlet_Discount_Summary.class);
						startActivityForResult(myIntent, 0);
					}
					
					else if(currentReportID.equals("5")){
						Intent myIntent = new Intent(view.getContext(),
								DM_BBM_Outlet_FreeIssue_Summary.class);
						startActivityForResult(myIntent, 0);
					}
				}
				else{
					final AlertDialog alertDialog = new AlertDialog.Builder(
							DM_BBM_Reports_Generator.this).create();
					// Setting Dialog Title
					alertDialog.setTitle("INVALID REPORT");
					// Setting Dialog Message
					alertDialog.setMessage("Plese Select Report From The List");
					// Setting Icon to Dialog
					alertDialog.setIcon(R.drawable.warning);
					// Setting OK Button
					alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							alertDialog.dismiss();
						}
					});
					// Showing Alert Message
					alertDialog.show();
					alertDialog.setCanceledOnTouchOutside(false);
					return;
				}
				
			}
		});
	}

	protected void resetFilters() {
		// TODO Auto-generated method stub
		etcurrentreport.setText("");
		btndmreportfromdate.setText("");
		btndmreporttodate.setText("");
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {
		case DATE_DIALOG_ID_FROM:
			// create a new DatePickerDialog with values you want to show
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		case DATE_DIALOG_ID_TO:
			// create a new DatePickerDialog with values you want to show
			return new DatePickerDialog(this, mDateSetListenerTo, mYear,
					mMonth, mDay);
		}
		return InvoiceHeaderAlert;
	}
	
	// Register DatePickerDialog listener
		private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
			// the callback received when the user "sets" the Date in the
			// DatePickerDialog
			public void onDateSet(DatePicker view, int yearSelected,
					int monthOfYear, int dayOfMonth) {
				mYear = yearSelected;
				mMonth = monthOfYear + 1;
				mDay = dayOfMonth;
				String mMonthStr = Integer.toString(mMonth);
				String mDayStr = Integer.toString(mDay);
				if (mMonthStr.length() == 1) {
					mMonthStr = "0" + mMonthStr;
				}
				if (mDayStr.length() == 1) {
					mDayStr = "0" + mDayStr;
				}
				btndmreportfromdate
						.setText(mDayStr + "/" + mMonthStr + "/" + mYear);
				mMonth = mMonth - 1;
			}
		};
		
		// Register DatePickerDialog listener
		private DatePickerDialog.OnDateSetListener mDateSetListenerTo = new DatePickerDialog.OnDateSetListener() {
			// the callback received when the user "sets" the Date in the
			// DatePickerDialog
			public void onDateSet(DatePicker view, int yearSelected,
					int monthOfYear, int dayOfMonth) {
				mYear = yearSelected;
				mMonth = monthOfYear + 1;
				mDay = dayOfMonth;
				String mMonthStr = Integer.toString(mMonth);
				String mDayStr = Integer.toString(mDay);
				if (mMonthStr.length() == 1) {
					mMonthStr = "0" + mMonthStr;
				}
				if (mDayStr.length() == 1) {
					mDayStr = "0" + mDayStr;
				}
				btndmreporttodate.setText(mDayStr + "/" + mMonthStr + "/" + mYear);
				mMonth = mMonth - 1;
			}
		};

		@Override
		public void onBackPressed() {
			// Do Here what ever you want do on back press;
			
		}
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case R.id.action_search:
				// search action
				return true;
			case R.id.action_location_found:
				// location found
				LocationFound();
				return true;
			case R.id.action_refresh:
				// refresh
				/*
				 * refreshMenuItem = item; // load the data from server new
				 * SyncData().execute();
				 */
				return true;
			case R.id.action_help:
				// help action
				return true;
			case R.id.action_check_updates:
				// check for updates action
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}

		private void LocationFound() {
			// TODO Auto-generated method stub
			Intent i = new Intent(DM_BBM_Reports_Generator.this,
					DM_BBM_InvoiceMenu.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(i);
			finish();
			return;
		}

		@Override
		public boolean onNavigationItemSelected(int itemPosition, long itemId) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// Inflate the menu; this adds items to the action bar if it is present.
			getMenuInflater().inflate(R.menu.activity_main_actions, menu);
			return true;
		}

}
